<?php
/*
$JA#COPYRIGHT$
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

class modJAJobsCounter{
	function get_list_jobs( $model, $mode='', $fieldlist, $params ){
		global $jbconfig, $mainframe;
		$arrReturn  = array();
		
		// Nuber format
		$thousand_separator = $params->get("thousand_separator", ",");
		// Cache proccess
		$update_interval	= intval( trim( $params->get("update_interval", "5") ) );
		// File name
		$fileName			= dirname(__FILE__).DS."counter".DS.$fieldlist->field_name.".txt";
		
		switch ( $update_interval ){
			case 0:
				break;
			case 1:
			case 3:			
			case 6:
			case 12:
				if ( file_exists( $fileName ) )
					if ( modJAJobsCounter::checkCacheTime($fileName, $update_interval) )
						return unserialize( file_get_contents($fileName) );
				break;
			case 13:
				if ( file_exists( $fileName ) )					
					return unserialize( file_get_contents($fileName) );
				break;
		}
		
		$data 			= "";
		if( $fieldlist->is_referred ){
			$db = JFactory::getDBO ();
			$catLevel = $params->get("category_level", "parent_id=0");
			$query = "SELECT {$fieldlist->referred_key} as value, {$fieldlist->referred_value} as text FROM #__{$fieldlist->referred_table} WHERE {$catLevel} ORDER BY {$fieldlist->referred_value} ASC";
			$db->setQuery ( $query );
			$data = $db->loadObjectList ();
		}else {
			$data = get_codes_of_field('ja_jobs_'.$fieldlist->field_name, " ORDER BY text ASC");			
		}
		
		if( empty($data) ) 
			return ;
		
		$where_more = " and a.status='Approved' ";
		$where_more .= ' and(a.effected_date<=now()) and ( (DATE_ADD(a.effected_date, INTERVAL '. (int)$jbconfig['posts']->get('posts_display_days', 30). ' DAY) >= now()  AND a.is_hotjob=0) or (DATE_ADD(a.effected_date, INTERVAL '. (int)$jbconfig['posts']->get('posts_show_days_elapsed', 30). ' DAY) >= now()  AND a.is_hotjob=1) )';
		$all_items = $model->getItems($where_more, '', 0, '', true);
		
		$i=0;
		foreach ($data as $k){
			$count = 0;
			if ( is_array($all_items) )
				foreach ($all_items as $index => $item){
					$arr = explode(",", $item->{$fieldlist->field_name});				
					if( in_array( $k->text, $arr) || in_array( $k->value, $arr) ){
						$count++;
						// unset( $all_items[$index] );
					}
				}
			
			$arrReturn[$i] 		= new stdClass();
			$arrReturn[$i]->text 	= $k->text;
			$arrReturn[$i]->num 	= number_format( $count, null, null, $thousand_separator);
			$arrReturn[$i]->field = $fieldlist->field_name;
			$i++;
		}
		
		// Store in file
		if ( $update_interval > 0 ){
			jimport('joomla.filesystem.file');
			JFile::write($fileName, serialize($arrReturn));
		}
		
		return $arrReturn;	
	}
	
	function checkCacheTime($file, $days){
		$fileCreateTime = filectime($file);
		$fileLimitTime	= time() - $days*60*60;
		return ( $fileLimitTime <= $fileCreateTime );
	}
	
	function getFile($name, $modPath, $tmplPath) {
		if (file_exists(JPATH_SITE.DS.$tmplPath.$name)) {
			return $tmplPath.$name;
		}
		return $modPath.$name;
	}
	
	function _findItem()
	{
		$needles = array(
			'view' 		=> 'jajobs',
			'layout'  	=> 'jalist'
		);
		$component =& JComponentHelper::getComponent('com_jajobboard');

		$menus	= &JApplication::getMenu('site', array());
		$items	= $menus->getItems('componentid', $component->id);

		$match 	= null;
		
		if (is_array($items))
		foreach($items as $item)
		{
			if ((@$item->query['view'] == 'jajobs') && (@$item->query['layout'] == 'jalist')) {
				$match = $item;
				break;
			}
		}
		
		if($match)	return $match->id;
		return null;
	}
}